<template>
  <div class="hawkContent">
    <!-- 面包屑 -->
    <hawk-breadcrumb></hawk-breadcrumb>
    <!-- 搜索、添加区域 -->
    <el-row :gutter="20" style="padding-left: 20px;">
      <el-col :span="3">
        <el-input
          placeholder="请输入名称搜索"
          v-model="queryForm.query.tname"
        ></el-input>
      </el-col>
      <el-col :span="3">
        <el-input
          placeholder="请输入版本搜索"
          v-model="queryForm.query.tedition"
        ></el-input>
      </el-col>

      <el-col :span="4">
        <el-button type="hawkSearch" @click="search()">搜索</el-button>
        <el-button plain @click="reSet()">重置</el-button>
      </el-col>
      <el-col
        :span="4"
        style="float: right;text-align: right;padding-right: 30px"
      >
        <el-button type="hawkCreate" @click="editSystem"
          >创建操作系统</el-button
        >
        <el-button plain>导入</el-button>
      </el-col>
    </el-row>
    <!-- 引入表格组件 -->
    <hawk-table
      :table-data="tableData"
      :page="queryForm.page"
      @selectionChange="handleSelectionChange"
      :options="[
        { key: 'tname', label: '名称' },
        { key: 'tedition', label: '版本' },
        { key: 'handle', label: '操作' }
      ]"
    >
      <template #handle="{ row }">
        <span class="hawk-text-btn" @click="editSystem(row)">编辑</span>
        <span class="hawk-text-divider">|</span>
        <span class="hawk-text-btn" @click="delSystem(row)">删除</span>
        <span class="hawk-text-divider">|</span>
        <span class="hawk-text-btn" @click="editSystem(row, 1)">查看</span>
      </template>
    </hawk-table>
    <!-- 表格底部 -->
    <div class="bottom-btns">
      <div class="bottom-btns-left">
        <div class="bottom-btns-left-num">
          {{ multipleSelection.length }} 选择
        </div>
        <el-button type="hawkDelete">批量删除</el-button>
        <el-button plain>导出</el-button>
      </div>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        v-if="page.size >= tableData.length"
        layout=" prev, pager, next,sizes"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 添加和修改模态框 -->
    <el-dialog
      :title="formTitle"
      :visible.sync="addDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="addDialogClosed"
    >
      <!-- 内容主题区 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="名称" prop="tname">
          <el-input
            :readonly="formTitle == '查看操作系统'"
            v-model="addForm.tname"
          ></el-input>
        </el-form-item>
        <el-form-item label="版本" prop="tedition">
          <el-input v-model="addForm.tedition"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/*
 * @author 秦秋雅
 * @description 资产操作系统表组件的视图层（增删改查）
 * @api 192.168.0.68:8082/cmdb-system/
 * @since 2021-10-10
 * @update 2021-10-14
 * @modifier 王洪展
 * @bug 无
 * @version v1.0.0
 */
import {
  getList,
  doDelete,
  doAdd,
  doEdit,
  search
} from '@/api/assets/list/system'
import HawkTable from '@/components/table'
import HawkBreadcrumb from '@/components/breadcrumb/Breadcrumb'

export default {
  components: { HawkBreadcrumb, HawkTable },
  //import引入的组件需要注入到对象中才能使用",
  data() {
    return {
      tableData: [],
      formTitle: '添加操作系统',
      addDialogVisible: false,
      multipleSelection: [],
      queryForm: {
        query: {
          tedition: '',
          tname: ''
        }
      },
      addForm: {
        tname: '',
        tedition: ''
      },
      addFormRules: {
        tname: [
          { required: true, message: '请输入操作系统名称', trigger: 'blur' }
        ],
        tedition: [{ required: true, message: '请输入版本', trigger: 'blur' }]
      },
      total: 0,
      page: {
        current: 1,
        size: 10
      }
    }
  },
  //监听属性 类似于data概念",
  computed: {
    cellStyle() {
      return {
        padding: '8px 0'
      }
    }
  },
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    // 搜索
    async search() {
      if (
        this.queryForm.query.tedition == '' &&
        this.queryForm.query.tname == ''
      ) {
        this.$message({
          type: 'warning',
          message: '请输入搜索信息！'
        })
        return
      }
      const res = await search(
        this.queryForm.query.tedition,
        this.queryForm.query.tname
      )
      console.log(res)
      this.tableData = res
      this.total = res.length
    },
    //重置搜索信息
    reSet() {
      this.queryForm = this.$options.data().queryForm
      this.init()
    },
    //获取列表
    async init() {
      const res = await getList(this.page)
      this.tableData = res.list
      this.total = res.count
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 每页数量修改
    handleSizeChange(val) {
      this.page.size = val
      this.init()
    },
    // 页码跳转
    handleCurrentChange(val) {
      this.page.current = val
      this.init()
    },
    // 执行添加和编辑
    save() {
      this.$refs.addFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        if (this.addForm.id) {
          await doEdit(this.addForm)
        } else {
          await doAdd(this.addForm)
        }
        this.addDialogVisible = false
        this.init()
      })
    },
    // 删除
    delSystem(row) {
      this.$confirm('此操作将删除该操作系统, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          doDelete(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          console.log(this.tableData.length)
          if (this.tableData.length == 1) {
            this.page.current = this.page.current - 1
            if (this.page.current < 1) {
              this.page.current = 1
            }
          }
          this.init()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加、编辑、查看方法
    editSystem(row, type) {
      if (!row.id) {
        this.formTitle = '添加操作系统'
      } else {
        if (type) {
          this.formTitle = '查看操作系统'
          this.addForm = Object.assign({}, row)
        } else {
          this.formTitle = '修改操作系统'
          this.addForm = Object.assign({}, row)
        }
      }
      this.addDialogVisible = true
    },
    // 模态框状态
    addDialogClosed() {
      this.addForm = this.$options.data().addForm
      // 模态框关闭时重新请求数据
      this.init()
      this.addDialogVisible = false
      this.$refs.addFormRef.resetFields()
    }
  },
  //生命周期 - 创建之前",数据模型未加载,方法未加载,html模板未加载
  beforeCreate() {},

  //生命周期 - 创建完成（可以访问当前this实例）",数据模型已加载，方法已加载,html模板已加载,html模板未渲染
  created() {
    this.init()
  },

  //生命周期 - 挂载完成（可以访问DOM元素）",html模板已渲染
  mounted() {}
}
</script>

<style lang="scss">

</style>
